package com.ht.impl.drawtool;

import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-6-19
 */
public class ArcTool extends RectTool {
    public static final String START_ANGLE = "startAngle";
    public static final String RECT_POSITION = "rectPosition";
    public static final int POSITION_LEFT_TOP = 1; //left top
    public static final int POSITION_LEFT_BOTTOM = 2; //left bottom
    public static final int POSITION_RIGHT_TOP = 3; //right top
    public static final int POSITION_RIGHT_BOTTOM = 4; //right bottom

    public int index;

    public ArcTool() {
        this("shape=htShapeArc");
    }

    public ArcTool(String style) {
        super(style);
        if (style == null)
            setStyle("");
        if (!getStyle().endsWith(";"))
            setStyle(getStyle() + ";");
        index = getStyle().length();
    }

    public void updateRect(MouseEvent e) {
        super.updateRect(e);
        boolean isClockwise = e.isControlDown();
        style = style.substring(0, index);

        int startAngle = 0;
        int position = POSITION_LEFT_TOP;
        if (isClockwise) {
            if (assistPoint.getX() > startPoint.getX() && assistPoint.getY() > startPoint.getY()) {
                position = POSITION_RIGHT_TOP;
                startAngle = 0;
            } else if (assistPoint.getX() > startPoint.getX() && assistPoint.getY() < startPoint.getY()) {
                startAngle = 90;
            } else if (assistPoint.getX() < startPoint.getX() && assistPoint.getY() < startPoint.getY()) {
                position = POSITION_LEFT_BOTTOM;
                startAngle = 180;
            } else if (assistPoint.getX() < startPoint.getX() && assistPoint.getY() > startPoint.getY()) {
                position = POSITION_RIGHT_BOTTOM;
                startAngle = -90;
            }
        } else {
            //right bottom
            if (assistPoint.getX() > startPoint.getX() && assistPoint.getY() > startPoint.getY()) {
                position = POSITION_LEFT_BOTTOM;
                startAngle = 180;
            } else if (assistPoint.getX() > startPoint.getX() && assistPoint.getY() < startPoint.getY()) {
                position = POSITION_RIGHT_BOTTOM;
                startAngle = -90;
            } else if (assistPoint.getX() < startPoint.getX() && assistPoint.getY() < startPoint.getY()) {
                position = POSITION_RIGHT_TOP;
                startAngle = 0;
            } else if (assistPoint.getX() < startPoint.getX() && assistPoint.getY() > startPoint.getY()) {
                startAngle = 90;
            }
        }
        style += (START_ANGLE + "=" + startAngle + ";" + RECT_POSITION + "=" + position);
        cell.setStyle(style);
    }
}
